//
//  ChatCellView.m
//  JiaoX
//
//  Created by yinqy on 15/3/16.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ChatCellView.h"
#import "ChatContentView.h"
#import "JXMinePostsCtl.h"
#import "JXUserInfoCtl.h"

@interface ChatCellView()
{
    UILabel *_timeLabel;
    UILabel *_nameLabel;
    UIImageView *_headImg;
    ChatContentView *_contentView;
    UIButton *_failedBtn;
}

@end
@implementation ChatCellView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _headImg = [[UIImageView alloc]init];
        [self addSubview:_headImg];
        
        _timeLabel = [[UILabel alloc]init];
        [_timeLabel setFont:TIME_LEBAL_FONT];
        [_timeLabel setTextColor:JXColor(102, 102, 102)];
        [_timeLabel setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:_timeLabel];
        
        _nameLabel = [[UILabel alloc]init];
        [_nameLabel setFont:NAME_LEBAL_FONT];
        [_nameLabel setTextColor:JXColor(153, 153, 153)];
        [self addSubview:_nameLabel];
        
//        _contentBtn = [[UIButton alloc]init];
//        [_contentBtn setTitleColor:JXColor(51, 51, 51) forState:UIControlStateNormal];
//        [_contentBtn.titleLabel setFont:CONTENT_LEBAL_FONT];
//        [_contentBtn.titleLabel setNumberOfLines:0];
        _contentView = [[ChatContentView alloc]init];
        [self addSubview:_contentView];
        
        _failedBtn = [[UIButton alloc]init];
        [_failedBtn setImage:IMG(@"ic_send_failed") forState:UIControlStateNormal];
        [self addSubview:_failedBtn];
    }
    return self;
}

-(void)setViewFrame:(ChatCellViewFrame *)viewFrame{
    _viewFrame = viewFrame;
    JXMessage *msg = viewFrame.message;
    
    self.frame = viewFrame.frame;
    
    if (msg.preTimestamp == 0 || (msg.timestamp-msg.preTimestamp)>1000*60*2) {
        [_timeLabel setFrame:viewFrame.timeLabelFrame];
        [_timeLabel setHidden:NO];
        NSString *timeStr = [Utils millisecs2DateString:msg.timestamp];
        [_timeLabel setText:timeStr];
        
    }else{
         [_timeLabel setHidden:YES];
    }
    
    if ([Utils isBlankString:msg.groupId]) {
        //代表是个人聊天
        [_nameLabel setHidden:YES];
        
    }else{
        //代表是群组聊天,显示nameLabel
        [_nameLabel setHidden:NO];
        [_nameLabel setFrame:viewFrame.nameLabelFrame];
    }
    
    [_headImg setFrame:viewFrame.headImgFrame];
    [_headImg setBackgroundColor:[UIColor redColor]];
    [_headImg.layer setCornerRadius:_headImg.height/2];
    [_headImg.layer setMasksToBounds:YES];
    
    [_contentView setFrame:viewFrame.contentLabelFrame];
    [_contentView setHidden:NO];
    if ([CURRENTUSERID isEqualToString:msg.fromPeerId]){
        if (msg.msgType == MsgTypeText) {
            [_contentView setText:[msg actualContent] resType:MessageResTypeTo];
        }else if(msg.msgType == MsgTypeImage){
            [_contentView setImage:msg resType:MessageResTypeTo];
        }
    }else{
        if (msg.msgType == MsgTypeText) {
            [_contentView setText:[msg actualContent] resType:MessageResTypeFrom];
        }else if(msg.msgType == MsgTypeImage){
            [_contentView setImage:msg resType:MessageResTypeFrom];
        }
    }
    if (msg.sendStatus == StatusSendFailed) {
        [_failedBtn setFrame:viewFrame.failedBtnFrame];
        [_failedBtn setHidden:NO];
    }else{
        [_failedBtn setHidden:YES];
    }
    
    _headImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerImgClick:)];
    [_headImg addGestureRecognizer:tap];
    
}
-(void)headerImgClick:(UITapGestureRecognizer *)ges{
    if ([_userinfo.userId isEqualToString:CURRENTUSERID]) {
        JXMinePostsCtl *ctl = [[JXMinePostsCtl alloc]initWithModelList:nil];
        [self.viewController.navigationController pushViewController:ctl animated:YES];
    }else{
        JXUserInfoCtl *ctl = [[JXUserInfoCtl alloc]initWithUserId:_userinfo.userId];
        [self.viewController.navigationController pushViewController:ctl animated:YES];
    }
}

-(void)setUserinfo:(UserInfo *)userinfo{
    _userinfo = userinfo;
    [_nameLabel setText:userinfo.nickName];
    [_headImg sd_setImageWithURL:[NSURL URLWithString:userinfo.userImg]];
}


@end
